// pages/recommend/components/choiceness/choiceness.js
const http = require("../../../../utils/http.js")

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    isHidden: {
      type: Boolean,
      value: true
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    cardList: [],

    pageIndex: 1,
    pageSize: 5,
    totalPage: 0,

    version: '1.0.0'
  },
  lifetimes: {
    attached: function() {
      // 在组件实例进入页面节点树时执行
      http.get("/get")
        .then(res => {
          this.setData({
            version: res.data,
          })
        })
      this.getData();
    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行

    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    getData() {
      let that = this
      that.setData({
        pageIndex: 1,
        pageSize: 5,
        totalPage: 0
      })
      let param = {
        pageIndex: that.data.pageIndex,
        pageSize: that.data.pageSize
      }
      http.get("/route/getRecommend", param)
        .then(res => {
          that.setData({
            totalPage: res.data.data.totalPage,
            cardList: res.data.data.resultList
          })
        })
    },
    addData() {
      let that = this
      if (that.data.pageIndex >= that.data.totalPage) {
        return;
      }

      that.setData({
        pageIndex: ++that.data.pageIndex
      })
      let param = {
        pageIndex: that.data.pageIndex,
        pageSize: that.data.pageSize
      }
      http.get("/route/getRecommend", param)
        .then(res => {
          let list = that.data.cardList
          list = list.concat(res.data.data.resultList)
          that.setData({
            totalPage: res.data.data.totalPage,
            cardList: list
          })
        })
    },
    gotoRoute(e) {
      let routeId = e.currentTarget.dataset.routeid
      wx.navigateTo({
        url: '/pages/recommend/components/choiceness/route/route?routeId=' + routeId,
      })
    }
  }
})